package com.yuan.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.reggie.common.R;
import com.yuan.reggie.pojo.Category;
import com.yuan.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yuanxingke
 * @date 2022/7/31 23:34
 */
@RestController
@Slf4j
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @PostMapping
    public R<String> add(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功");
    }

    @GetMapping("page")
    public R<Page<Category>> page(Integer page,Integer pageSize){

        Page<Category> categoryPage = new Page<>(page,pageSize);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        categoryService.page(categoryPage,queryWrapper);
        return R.success(categoryPage);
    }

    @DeleteMapping
    public R<String> remove(Long id){

        categoryService.remove(id);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }


    /**
     * 根据类型查询类型所有的菜品类型
     * @param type
     * @return
     */
    @GetMapping("list")
    public R list(Integer type){

        LambdaQueryWrapper<Category> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(type!=null,Category::getType,type).orderByAsc(Category::getSort);

        //使用list方法查询数据
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return R.success(list);
    }


}
